package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.exception.CustomException;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.domain.vo.SysDepositVo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysFundaccountAddBo;
import com.xjy.system.domain.bo.SysFundaccountQueryBo;
import com.xjy.system.domain.bo.SysFundaccountEditBo;
import com.xjy.system.domain.SysFundaccount;
import com.xjy.system.mapper.SysFundaccountMapper;
import com.xjy.system.domain.vo.SysFundaccountVo;
import com.xjy.system.service.ISysFundaccountService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资金账户信息Service业务层处理
 *
 * @author xjy
 * @date 2022-03-10
 */
@Service
public class SysFundaccountServiceImpl extends ServicePlusImpl<SysFundaccountMapper, SysFundaccount> implements ISysFundaccountService {

	@Override
	public SysFundaccountVo queryById(Long id) {
		return getVoById(id, SysFundaccountVo.class);
	}

	@Override
	public TableDataInfo<SysFundaccountVo> queryPageList(SysFundaccountQueryBo bo) {
		PagePlus<SysFundaccount, SysFundaccountVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysFundaccountVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysFundaccountVo> queryList(SysFundaccountQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysFundaccountVo.class);
	}

	private LambdaQueryWrapper<SysFundaccount> buildQueryWrapper(SysFundaccountQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysFundaccount> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getAccountId() != null, SysFundaccount::getAccountId, bo.getAccountId());
		lqw.eq(bo.getProjId() != null, SysFundaccount::getProjId, bo.getProjId());
		lqw.like(StrUtil.isNotBlank(bo.getAccountName()), SysFundaccount::getAccountName, bo.getAccountName());
		lqw.like(StrUtil.isNotBlank(bo.getProjName()), SysFundaccount::getProjName, bo.getProjName());
		lqw.eq(StrUtil.isNotBlank(bo.getAccountBank()), SysFundaccount::getAccountBank, bo.getAccountBank());
		lqw.eq(StrUtil.isNotBlank(bo.getAccountPhone()), SysFundaccount::getAccountPhone, bo.getAccountPhone());
		lqw.eq(bo.getAccountFistbalance() != null, SysFundaccount::getAccountFistbalance, bo.getAccountFistbalance());
		lqw.eq(bo.getAccountLastbalance() != null, SysFundaccount::getAccountLastbalance, bo.getAccountLastbalance());
		lqw.eq(bo.getAuditStatus() != null, SysFundaccount::getAuditStatus, bo.getAuditStatus());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysFundaccountAddBo bo) {
		SysFundaccount add = BeanUtil.toBean(bo, SysFundaccount.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysFundaccountEditBo bo) {
		SysFundaccount update = BeanUtil.toBean(bo, SysFundaccount.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	@Override
	public Boolean updateAccountLastbalance(SysFundaccountEditBo bo) {
		Long projId = bo.getProjId();
		Long accountId = bo.getAccountId();
		BigDecimal accountLastbalance = bo.getAccountLastbalance();
		if(null == accountId){
			throw new CustomException("更新余额时，账号ID不能为空！");
		}
		if(null == projId){
			throw new CustomException("更新余额时，项目ID不能为空！");
		}
		if(null == accountLastbalance || accountLastbalance.compareTo(new BigDecimal(0)) < 0){
			throw new CustomException("更新余额时，余额必须大于0！");
		}
		SysFundaccount sysFundaccount = new SysFundaccount();
		sysFundaccount.setProjId(projId);
		sysFundaccount.setAccountId(accountId);
		sysFundaccount.setAccountLastbalance(accountLastbalance);
		return baseMapper.updateAccountLastbalance(sysFundaccount);
	}

	@Override
	public Boolean auditByEditBo(SysFundaccountEditBo bo) {
		SysFundaccountVo sysFundaccountVo = this.queryById(bo.getId());
		if (sysFundaccountVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}

		bo.setAuditStatus(true);
		bo.setAccountLastbalance(bo.getAccountFistbalance());
		return this.updateByEditBo(bo);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysFundaccount entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
